//
//  NoteTabButton.m
//  Digital Seamonsters
//
//  Created by Adam Cue on 1/29/11.
//  Copyright Adam Cue 2011.
//
//  This program is licensed and distributed under the terms of the GNU General Public License.  
//	The text for the license can be found in the license.txt file in this project.
//

#import "NoteTabButton.h"

@interface NoteTabButton (Private)
#pragma mark HelperMethods
- (void)showNote;
- (void)animateNote:(UIGestureRecognizer *)gestureRecognizer;
@end

@implementation NoteTabButton

@synthesize note = _note;
@synthesize noteLabel = _noteLabel;
@synthesize delegate;

- (id)initWithCoder:(NSCoder *)aDecoder {
   self = [super initWithCoder:aDecoder];
   if (self) {
      UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNote)];
      [self addGestureRecognizer:tap];
      [tap release];
      UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(animateNote:)];
      [self addGestureRecognizer:longPress];
      [longPress release];
   }
   return self;
}

- (void)dealloc {
   self.note = nil;
   self.delegate = nil;
   
   [super dealloc];
}

#pragma mark HelperMethods
- (void)showNote {
	[delegate showNote:self.note.text button:self];
}

- (void)animateNote:(UIGestureRecognizer *)gestureRecognizer {
   if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
      [UIView animateWithDuration:0.15 delay:0.0 options:UIViewAnimationCurveLinear 
                       animations:^{
                          self.transform = CGAffineTransformMakeScale(1.2, 1.2);
                       } completion:^(BOOL finished){
                          [UIView animateWithDuration:0.15 delay:0.0 options:UIViewAnimationCurveLinear 
                                           animations:^{
                                              self.transform = CGAffineTransformMakeScale(1.1, 1.1);
                                           }completion:nil];
                       }];
   }
   else if(gestureRecognizer.state == UIGestureRecognizerStateChanged){
      self.center = CGPointMake(self.center.x, [gestureRecognizer locationInView:self.superview].y);
   }
   else{
      self.note.x = [NSNumber numberWithInt:self.origin.x];
      self.note.y = [NSNumber numberWithInt:self.origin.y];
      self.transform = CGAffineTransformIdentity;
   }
}

#pragma mark NoteViewControllerDelegate
- (void)noteEmailed:(NSString *)selectedText{
	[delegate noteEmailed:selectedText];
}

- (void)noteDismissed:(NSString *)selectedText{
	self.note.text = selectedText;
	self.noteLabel.text = selectedText;
}

- (void)noteTrashed{
	[delegate noteDeleted:self.note];
	[self removeFromSuperview];
}

@end
